package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.dal.mapper.FreezeMapper;
import com.baofu.paycore.dal.mapper.PreAuthMapper;
import com.baofu.paycore.dal.model.FreezeDO;
import com.baofu.paycore.dal.model.PreAuthDO;
import com.baofu.paycore.manager.storage.Storage;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 预授权主单存储
 *
 * <p/>
 *      1、新增订单
 *      2、更新订单
 *      3、废弃订单
 * </p>
 * User: xin deng  Date: 16/8/30 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Repository
public class FreezeManager implements Storage<FreezeDO> {

    /**
     * 预授权底层Mapper
     */
    @Autowired
    private FreezeMapper freezeMapper;

    /**
     * 新增订单
     *
     * @param freezeDO 订单信息
     */
    @Override
    public int addOrder(FreezeDO freezeDO) {
        return freezeMapper.insert(freezeDO);
    }

    /**
     * 更新订单
     *
     * @param freezeDO 订单信息
     */
    @Override
    public int modifyOrder(FreezeDO freezeDO) {

        return freezeMapper.update(freezeDO);
    }

    /**
     * 废弃订单
     *
     * @param freezeDO 订单信息
     */
    @Override
    public int deleteOrder(FreezeDO freezeDO) {
        return 0;
    }

    /**
     * 分页查询预授权信息
     *
     * @param freezeDO     请求参数
     * @param beginPage     开始行
     * @param endPage       结束行
     * @return              预授权信息
     */
    public List<FreezeDO> selectPreAuthPage(FreezeDO freezeDO, Integer beginPage, Integer endPage){

        return freezeMapper.selectFreezePage(freezeDO, beginPage, endPage);
    }

    /**
     * 查询预授权信息
     *
     * @param freezeDO     请求参数
     * @return              总数
     */
    public int selectPreAuthCount(FreezeDO freezeDO){

        return freezeMapper.selectFreezeCount(freezeDO);
    }
    /**
     * 根据订单号、支付流水号查询交易
     *
     * @return 交易
     */
    public FreezeDO selectByOrderNoAndBizNo(String requestOrderNo, String requestBizNo, String requestSystem,
                                              Date requestDate){
        FreezeDO freezeDO = freezeMapper.selectByOrderNoAndBizNo(
                requestOrderNo, requestBizNo, requestSystem, requestDate);
        if (freezeDO == null) {
            log.error("查询结果集为空:{} {} {} {}", requestOrderNo, requestBizNo, requestSystem, requestDate);
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }
        return freezeDO;
    }

    /**
     * 根据支付单号查询冻结单
     * @param payNo
     * @return
     */
    public FreezeDO selectByPayNo(String payNo){
        return freezeMapper.selectByPayNo(payNo);
    }
}
